package com.hluck.materialstudy.ui.activity

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.widget.Toast
import androidx.core.view.GravityCompat
import androidx.databinding.DataBindingUtil
import com.google.android.material.snackbar.Snackbar
import com.hluck.materialstudy.R
import com.hluck.materialstudy.app.extension.showSnackMsg
import com.hluck.materialstudy.app.extension.showToast
import com.hluck.materialstudy.data.models.Fruit
import com.hluck.materialstudy.databinding.ActivityMainBinding
import com.hluck.materialstudy.ui.adapter.FruitAdapter
import kotlin.concurrent.thread


class MainActivity : AppCompatActivity() {

    private lateinit var mbinding:ActivityMainBinding
    private val fruits = mutableListOf<Fruit>(
        Fruit("Apple",R.drawable.apple),
        Fruit("Banana",R.drawable.banana),
        Fruit("Orange",R.drawable.orange),
        Fruit("Watermelon",R.drawable.watermelon),
        Fruit("Pear",R.drawable.pear),
        Fruit("Grape",R.drawable.grape),
        Fruit("Pineapple",R.drawable.pineapple),
        Fruit("Strawberry",R.drawable.strawberry),
        Fruit("Cherry",R.drawable.cherry),
        Fruit("Mango",R.drawable.mango)
    )

    private val list = ArrayList<Fruit>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mbinding = DataBindingUtil.setContentView(this, R.layout.activity_main)
        setSupportActionBar(mbinding.mainLayout.toolbar)
        supportActionBar?.let {
            it.setDisplayHomeAsUpEnabled(true)
            it.setHomeAsUpIndicator(R.drawable.ic_menu)
        }

        mbinding.drawerLayout.navigationView.setCheckedItem(R.id.nav_call)
        mbinding.drawerLayout.navigationView.setNavigationItemSelectedListener {
            when(it.itemId){
                R.id.nav_call -> "Call".showToast(this)
                R.id.nav_friends -> "Friends".showToast(this)
                R.id.nav_location -> "Location".showToast(this)
                R.id.nav_mail -> "Mail".showToast(this)
                R.id.nav_task -> "Task".showToast(this)
            }
            mbinding.drawerRootLayout.closeDrawer(GravityCompat.START)
            true
        }

        mbinding.mainLayout.fab.setOnClickListener {
            it.showSnackMsg("FloatingButton","UNDO"){
                "UNDO".showToast(it.context)
            }
        }

        initFruit()
        val gridLayoutManager = androidx.recyclerview.widget.GridLayoutManager(this,2)
        mbinding.mainLayout.recyclerView.layoutManager = gridLayoutManager
        mbinding.mainLayout.recyclerView.adapter = FruitAdapter(this,list)

        mbinding.mainLayout.swiperefreshLayout.setColorSchemeResources(R.color.colorPrimary)
        mbinding.mainLayout.swiperefreshLayout.setOnRefreshListener {
            refreshFruit()
        }
    }

    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.toolbar_menu,menu)
        return true
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {

        when(item.itemId){
            R.id.backup -> "Task".showToast(this)
            R.id.settings -> "Settings".showToast(this)
            R.id.delete -> "Delete".showToast(this)
            android.R.id.home -> mbinding.drawerRootLayout.openDrawer(GravityCompat.START)
        }

        return true
    }

    private fun initFruit(){
        list.clear()
        repeat(50){
            val index = (0 until fruits.size).random()
            list.add(fruits[index])
        }
    }


    private fun refreshFruit(){
        thread {
            Thread.sleep(1000)
            runOnUiThread {
                initFruit()
                mbinding.mainLayout.recyclerView.adapter?.notifyDataSetChanged()
                //表示刷新事件结束隐藏刷新进度条
                mbinding.mainLayout.swiperefreshLayout.isRefreshing = false
            }
        }
    }

}